--[[
    addPipes():创建4对上下对齐的管道，加入碰撞检测
    pipesScroll():滚动管道并计分
    generateX, removeX = 新管道的生成X坐标startX，管道移出屏幕后自动销毁的X坐标endX
    reset():重新开始游戏后重置管道
]]
local pipes = {}
local number = 1

cc.exports.createPipes = function()    
local generateX, removeX = nil, nil
local scene = cc.Director:getInstance():getRunningScene() 
local spriteLayer = scene:getChildByTag(TAG.SPRITE)
local score = 0
local count = spriteLayer:getChildByTag(TAG.COUNT)
local bird = spriteLayer:getChildByTag(TAG.BIRD)
local birdHeight = bird:getContentSize().height

    local function addPipes()
        local pipe = cc.Sprite:create("pipe_up.png")
        local width, length = pipe:getContentSize().width, pipe:getContentSize().height
        local gapX = SPACE_X * width
        local gapY =  SPACE_Y * birdHeight
        generateX = display.cx + bgWidth / 2 - gapX / 2
        removeX = display.cx - bgWidth / 2 - width / 2
        local x, startY = generateX, display.cy -bgHeight / 2 
        math.randomseed(os.time()) 

        for i = 1, 4 do
            local deltaY = bgHeight / 2 * (math.random() - 0.5)

            local up = cc.Sprite:create("pipe_up.png"):setTag(TAG.PIPE)
            local down = cc.Sprite:create("pipe_down.png"):setTag(TAG.PIPE)
            up:setAnchorPoint(0.5, 0)
            down:setAnchorPoint(0.5, 0)

            x = x + gapX + width
            local y1 = startY - gapY + deltaY
            up:setPosition(cc.p(x, y1))
            local y2 = up:getPositionY() + length + gapY 
            down:setPosition(cc.p(x, y2))
            up:setPhysicsBody(cc.PhysicsBody:createEdgeBox(cc.size(width, length)))
            up:getPhysicsBody():setContactTestBitmask(0x00000001)
            down:setPhysicsBody(cc.PhysicsBody:createEdgeBox(cc.size(width, length)))
            down:getPhysicsBody():setContactTestBitmask(0x00000001)

            pipes[number] = up
            pipes[number]:addTo(spriteLayer)
            number = number + 1

            pipes[number] = down
            pipes[number]:addTo(spriteLayer)
            number = number + 1
        end
    end

    local function pipesScroll(delta)
        if  pipes[table.maxn(pipes)]:getPositionX() <= generateX then
            addPipes()
        end
        
        local x, speed = 0, DELTAx
        for i, v in pairs(pipes) do
            repeat
                if v:getPositionX() <= removeX then
                    v:setVisible(false)
                    v:removeFromParent()
                    pipes[i] = nil
                    break
                end

                if nil ~= pipes[i+1] and i % 2 == 1 
                then
                    if v:getPositionX() <= bird:getPositionX() 
                        and 
                       v:getPositionX() + DELTAx >= bird:getPositionX()
                    then
                        score = score + 1
                        count:setString(score)
                    end
                end

                x = v:getPositionX() - speed
                v:setPositionX(x)
            until true
        end
    end

    local function reset()
        number = 1
        pipes = {}
    end

    return {addPipes = addPipes,
            pipesScroll = pipesScroll,
            reset = reset
        }
end
--endregion
